package com.example.administrator.im.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.im.R;
import com.example.administrator.im.config.ImageConfig;
import com.example.administrator.im.db.FriendDBHelper;
import com.example.administrator.im.interfacz.ShareTitleable;
import com.example.administrator.im.json.ResultJson;
import com.example.administrator.im.utils.DialogUtils;
import com.example.administrator.im.utils.HttpUtils;
import com.example.administrator.im.view.ShareTitle;
import com.google.gson.Gson;

import org.litepal.LitePal;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * create:2018/10/2 16:15
 * author: 姚鸿杰
 **/
public class ChatSettingActivity extends MySlideFinishActivity {

    private final String TAG = "ChatSettingActivity";

    private ShareTitle shareTitle;

    private String username = "";

    private Switch aSwitch;
    private CircleImageView chat_civ;
    private TextView chat_nickname;

    private FriendDBHelper friend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_setting);

        Intent intent = getIntent();
        if (intent != null)
            username = intent.getStringExtra("username");

        initView();
        loadData();
    }


    private void loadData() {

        friend = LitePal.where("username = ?", username).findFirst(FriendDBHelper.class);
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.login_logo)
                .error(R.mipmap.login_logo)
                .override(ImageConfig.getItemSmall(), ImageConfig.getItemSmall());
        Glide.with(this)
                .load(HttpUtils.PORT + friend.getImg_path())
                .apply(options).into(chat_civ);

        chat_nickname.setText(friend.getNickname());

        if (friend != null) {
            aSwitch.setChecked(friend.isDisturb());
        }
        final String friend_id = username;

        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {

                DialogUtils.setDialog(ChatSettingActivity.this, R.layout.loading);
                DialogUtils.showDialog();

                HttpUtils.editingFriendRequest(friend_id, HttpUtils.FRIEND_DISTURB,
                        String.valueOf(isChecked ? 1 : 0), new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                DialogUtils.clearAlerDialog();
                                Log.e(TAG, e.getMessage());
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                DialogUtils.clearAlerDialog();
                                String content = response.body().string();
                                Gson gson = new Gson();
                                ResultJson json = gson.fromJson(content, ResultJson.class);
                                if (json.getCode() == HttpUtils.SUCCESS) {
                                    friend.setDisturb(isChecked);
                                    friend.save();
                                } else {
                                    Log.e(TAG, json.getData().toString());
                                }
                            }
                        });
            }
        });

    }

    private void initView() {
        shareTitle = (ShareTitle) findViewById(R.id.chat_setting_toolbar);
        shareTitle.setIconOnClickListener(new ShareTitleable() {
            @Override
            public void left_click(View v) {
                finish();
            }

            @Override
            public void right_click(View v) {
            }
        });
        chat_civ = (CircleImageView) findViewById(R.id.chat_civ);
        chat_nickname = (TextView) findViewById(R.id.chat_nickname);
        aSwitch = (Switch) findViewById(R.id.disturb);
    }

    public void openFriendDetails(View v) {
        Intent intent = new Intent(this, FriendDetailsActivity.class);
        //传递参数
        intent.putExtra("username", friend.getUsername());
        intent.putExtra("nickname", friend.getNickname());
        startActivity(intent);
    }

}
